import {
  showLoadingToast,
  showSuccessToast,
  closeToast,
  showFailToast,
} from 'vant'
import i18n from "@/lang";

// 保留小数位
export const getFloat = (number = 0, decimalPlaces = 2) => {
  const roundedNumber =
    Math.floor(number * Math.pow(10, decimalPlaces)) /
    Math.pow(10, decimalPlaces)
  // const decimalPart = roundedNumber % 1 === 0 ? '' : ''
  const roundedString = roundedNumber.toFixed(decimalPlaces)
  return roundedString
}

// 浮点数的乘法运算
export const floatMul = (arg1, arg2) => {
  let m = 0,
    s1 = arg1.toString(),
    s2 = arg2.toString()
  try
  {
    m += s1.split('.')[1].length
  } catch (e) { }
  try
  {
    m += s2.split('.')[1].length
  } catch (e) { }
  return (
    (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) /
    Math.pow(10, m)
  )
}

// 授权  token 代币合约   con 授权合约地址
export const getDotcAllowance = async (address, token, conAddress) => {
  await token.methods
    .allowance(address, conAddress)
    .call()
    .then(async (res) => {

      if (res == 0)
      {
        await token.methods
          .approve(
            conAddress,
            '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
          )
          .send({
            from: address,
            gas: 200000,
            gasPrice: 110000000 //0.11Gwei
          })
          .on('transactionHash', (hash) => {
            showLoadingToast({
              message: i18n.global.t('hl_waitLink'),
              forbidClick: true,
              duration: 0,
            })
          })
          .on('receipt', (receipt) => {
            closeToast()
            showSuccessToast(i18n.global.t('hl_opSucc'))
          })
          .on('error', (error, receipt) => {
            closeToast()
            showFailToast(i18n.global.t('hl_opFail'))
          })
      }

    })
}

// 授权  token 代币合约   con 授权合约地址
export const getCtcAllowance = async (token, allowtoken, conAddress) => {
  await allowtoken.methods
    .allowance(sessionStorage.getItem('address'), conAddress)
    .call()
    .then(async (res) => {
      // console.log(res)
      if (res == 0)
      {
        await token.methods
          .approve(
            conAddress,
            '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
          )
          .send({
            from: sessionStorage.getItem('address'),
            gas: 200000,
            gasPrice: 110000000 //0.11Gwei
          })
          .on('transactionHash', (hash) => {
            showLoadingToast({
              message: i18n.global.t('hl_waitLink'),
              forbidClick: true,
              duration: 0,
            })
          })
          .on('receipt', (receipt) => {
            closeToast()
            showSuccessToast(i18n.global.t('hl_opSucc'))
          })
          .on('error', (error, receipt) => {
            closeToast()
            showFailToast(i18n.global.t('hl_opFail'))
          })
      }
    })
}


/**
 * 获取调用合约方法的大概耗费gas费
 * @param {any} methodData 要估算的合约方法，eg:  contract.methods.approve(address1,address2)
 * @param {String} fromAddress 调用合约方法的地址
 * @returns
 */
export const getEstimateGas = async (methodData, fromAddress) => {
  try
  {
    const gasEstimate = await methodData.estimateGas({
      from: fromAddress,
    });
    // // gas当前的价格
    // const gasPrice = await web3.eth.getGasPrice();
    // // 显示gas对应的ETH费用
    // this.gasFee = web3.utils.fromWei(
    //   (gasEstimate * gasPrice).toString(),
    //   "ether"
    // );
    return gasEstimate;
  } catch (error)
  {
    console.log("e :>> ", error);
    return 0;
  }
};

